﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 原料到发记录管理 : System.Web.UI.Page
    {
        protected string Notice = "";
        string sql =
@"select t1.ID
,t1.原料ID
,t1.供货ID
,case when t1.分厂=0 then '待定'
when t1.分厂=1 then '一分厂'
when t1.分厂=2 then '二分厂'
when t1.分厂=3 then '三分厂'
when t1.分厂=4 then '四分厂'
when t1.分厂=5 then '五分厂'
else '未知分厂'
end as 分厂
,t1.库区
,t1.到货日期
,t1.发货日期
,t1.总重量
,t1.磅重
,t1.总支数
,t1.质检员
,t1.状态
,case when t1.状态=0 then '未送检'
when t1.状态=1 then '待检'
when t1.状态=2 then '合格'
when t1.状态=3 then '已发货'
else '未知状态'end as 状态描述
,case when t1.原料ID=1 then '油管光管(J55)'
when t1.原料ID=2 then '套管光管(J55)'
when t1.原料ID=3 then '油管接箍光管(J55)'
when t1.原料ID=4 then '套管接箍光管(J55)'
when t1.原料ID=5 then '成品油管接箍(J55)'
when t1.原料ID=6 then '成品套管接箍(J55)'
when t1.原料ID=101 then '油管光管(N80Q)'
when t1.原料ID=102 then '套管光管(N80Q)'
when t1.原料ID=103 then '油管接箍光管(N80Q)'
when t1.原料ID=104 then '套管接箍光管(N80Q)'
when t1.原料ID=105 then '成品油管接箍(N80Q)'
when t1.原料ID=106 then '成品套管接箍(N80Q)'
else '其他原料' end as 原料
,t2.名称 as 供货单位
,t3.单据号 as 入库单
,t4.单据号 as 出库单
from 原料到发记录 t1
left outer join 原料供货单位 t2 on t2.ID=t1.供货ID
left outer join 原料入出库 t3 on t3.ID=t1.入库单ID
left outer join 原料入出库 t4 on t4.ID=t1.出库单ID
";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.GongHuo.DataBind();
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
            
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;
            this.Button_Post.Enabled = false;
            this.Button_CancelPost.Enabled = false;
            this.Button_Pass.Enabled = false;
            this.Button_CancelPass.Enabled = false;
            this.Button_Send.Enabled = false;
            this.Button_CancelSend.Enabled = false;
            this.Button_In.Enabled = false;
            this.Button_Out.Enabled = false;

            this.GridView1.SelectedIndex = -1;

            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        private string GetState()
        {
            string state = "";
            if (this.LeiBie.Text != "0")
            {
                state += " and t1.原料ID=" + this.LeiBie.Text;
            }

            if (this.GongHuo.Text != "0")
            {
                state += " and t1.供货ID=" + this.GongHuo.Text;
            }

            if (this.FenChang.Text != "0")
            {
                state += " and t1.分厂=" + this.FenChang.Text;
            }

            if (this.XianShi.Text != "")
            {
                state += " and t1.状态=" + this.XianShi.Text;
            }
            int kq;
            if(int.TryParse(this.KuQu.Text,out kq))
            {
                state += " and t1.库区=" + kq;
            }
            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.到货日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.到货日期<='" + d.ToShortDateString() + "'";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {

            string state = this.GetState();

            this.SqlText.Text = sql + state + " order by t1.ID desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {

            this.LeiBie.Text = "0";
            this.GongHuo.Text = "0";
            this.FenChang.Text = "0";
            this.KuQu.Text = "";

            this.XianShi.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";

        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.GetSelectedList();
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }

        private string GetSelectedList()
        {
            string s="";
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                if (((CheckBox)this.GridView1.Rows[i].FindControl("Check_Item")).Checked)
                {

                    s += ","+this.GridView1.DataKeys[i].Values[0].ToString();
                }
            }
            if (s.Length > 0)
            {
                s = s.Substring(1);
            }
            return s;
        }
        protected void Button_Post_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                this.SqlDataSource1.UpdateCommand = "update 原料到发记录 set 状态=1 where ID in (" + list + ") and 状态=0";
                this.Notice = "“" + ((Button)sender).Text + "”执行成功 " + this.SqlDataSource1.Update() + " 条";
            }
        }

        protected void Button_CancelPost_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                this.SqlDataSource1.UpdateCommand = "update 原料到发记录 set 状态=0 where ID in (" + list + ") and 状态=1";
                this.Notice = "“" + ((Button)sender).Text + "”执行成功 " + this.SqlDataSource1.Update() + " 条";
            }
        }

        protected void Button_Pass_Click(object sender, EventArgs e)
        {
            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                this.SqlDataSource1.UpdateCommand = "update 原料到发记录 set 状态=2,质检员='"+op+"' where ID in (" + list + ") and 状态=1";
                this.Notice = "“" + ((Button)sender).Text + "”执行成功 " + this.SqlDataSource1.Update() + " 条";
            }
        }

        protected void Button_CancelPass_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                this.SqlDataSource1.UpdateCommand = "update 原料到发记录 set 状态=1,质检员='' where ID in (" + list + ") and 状态=2";
                this.Notice = "“" + ((Button)sender).Text + "”执行成功 " + this.SqlDataSource1.Update() + " 条";
            }
        }

        protected void Button_Send_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/原料发货.aspx?id=" + list + "',600,300);</script>");
            }
        }

        protected void Button_CancelSend_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                this.SqlDataSource1.UpdateCommand = "set nocount on;update 原料到发记录 set 分厂=0 where ID in (" + list + ") and 状态=3 and 入库单ID=0 and 出库单ID=0 and 原料ID in (5,6,105,106);set nocount off;"
                    + "update 原料到发记录 set 状态=2,发货日期=null where ID in (" + list + ") and 状态=3 and 入库单ID=0 and 出库单ID=0;";
                this.Notice ="“"+((Button)sender).Text+ "”执行成功 " + this.SqlDataSource1.Update() + " 条";
            }
        }

        protected void Button_In_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/原料入库单录入.aspx?list=" + list + "',770,440);</script>");
            }
        }

        protected void Button_Out_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/原料出库单录入.aspx?list=" + list + "',770,440);</script>");
            }
        }
        protected void Check_All_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox s = (CheckBox)sender;
            string cs = "grid-row-normal";
            if (s.Checked)
            {
                cs = "grid-row-selected";
            }
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                ((CheckBox)this.GridView1.Rows[i].FindControl("Check_Item")).Checked = s.Checked;
                this.GridView1.Rows[i].CssClass = cs;
            }
            this.Button_Delete.Enabled = false;
            this.Set_Button_State();
        }
        protected void Check_Item_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox s = (CheckBox)sender;
            string cs = "grid-row-normal";
            if (s.Checked)
            {
                cs = "grid-row-selected";
            }

            ((GridViewRow)s.Parent.Parent).CssClass = cs;
            string list = GetSelectedList();
            if (list != "" && list.IndexOf(',') < 0)
            {
                this.Button_Delete.Enabled = true;
            }
            else
            {
                this.Button_Delete.Enabled = false;
            }
            this.Set_Button_State();
        }
        private void Set_Button_State()
        {
            if (GetSelectedList() == "")
            {
                this.Button_Edit.Enabled = false;
                this.Button_Post.Enabled = false;
                this.Button_CancelPost.Enabled = false;
                this.Button_Pass.Enabled = false;
                this.Button_CancelPass.Enabled = false;
                this.Button_Send.Enabled = false;
                this.Button_CancelSend.Enabled = false;
                this.Button_In.Enabled = false;
                this.Button_Out.Enabled = false;
            }
            else
            {
                this.Button_Edit.Enabled = true;
                this.Button_Post.Enabled = true;
                this.Button_CancelPost.Enabled = true;
                this.Button_Pass.Enabled = true;
                this.Button_CancelPass.Enabled = true;
                this.Button_Send.Enabled = true;
                this.Button_CancelSend.Enabled = true;
                this.Button_In.Enabled = true;
                this.Button_Out.Enabled = true;
            }
        }

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            string list = GetSelectedList();
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/原料到货记录录入.aspx?list=" + list + "',740,600,'yes');</script>");
            }
        }
    }
}
